package ads.server;

import java.lang.reflect.Proxy;
import java.net.URI;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;

import ads.exception.InvalideInvokerException;
import ads.exception.SpreadRemoteException;

public class SpreadRequestor implements SpreadMemberRemoteInterface {
	private static final Logger log = Logger.getLogger(SpreadRequestor.class
			.getName());
	private SpreadMember spreadMember;
	private SpreadMemberRemoteInterface proxy;

	public SpreadMemberRemoteInterface getProxy() {
		if (proxy == null) {
			proxy = createProxy();
		}
		return proxy;
	}

	private SpreadMemberRemoteInterface createProxy() {
		ClassLoader cl = SpreadMemberRemoteInterface.class.getClassLoader();
		return (SpreadMemberRemoteInterface) Proxy.newProxyInstance(cl,
				new Class[] { SpreadMemberRemoteInterface.class },
				new SpreadRequestHandler(spreadMember));
	}

	public SpreadRequestor(SpreadMember spreadMember) {
		super();
		this.spreadMember = spreadMember;
		proxy = createProxy();

	}

	@Override
	public String queryPrimaryServer() throws SpreadRemoteException,
			InvalideInvokerException {
		return getProxy().queryPrimaryServer();
	}

	@Override
	public URI getPrimaryServerURI() throws SpreadRemoteException,
			InvalideInvokerException {
		return getProxy().getPrimaryServerURI();
	}

	@Override
	public void updateServers(String primaryServer) throws SpreadRemoteException {
		log.debug(spreadMember.privateServerGroup+" send server list to all server members");
		if (spreadMember.IsPrimaryServer())
			getProxy().updateServers(primaryServer);

	}


}
